<?php
/**
 * ExtMessageBox class file
 *
 * @author TrueSoftware B.V. (The Netherlands)
 * @link http://www.ext4yii.com
 * @copyright Copyright 2007-2010 TrueSoftwareB.V. all rights reserved.
 * @license http://www.ext4yii.com/license/
 *          EVALUATION VERSION - NOT FOR COMMERCIAL USE OR DISTRIBUTION.
 * @version 0.7.2
 */

class ExtMessageBox
{
    public static $BUTTON_OK = 'Ext.MessageBox.OK';
    public static $BUTTON_YESNO = 'Ext.MessageBox.YESNO';
    public static $BUTTON_OKCANCEL = 'Ext.MessageBox.OKCANCEL';
    public static $BUTTON_YESNOCANCEL = 'Ext.MessageBox.YESNOCANCEL';

    public static $ICON_QUESTION = 'Ext.MessageBox.QUESTION';
    public static $ICON_WARNING = 'Ext.MessageBox.WARNING';
    public static $ICON_ERROR = 'Ext.MessageBox.ERROR';
    public static $ICON_INFO = 'Ext.MessageBox.INFO';

    /**
     * Creates a server-size alert message box to be rendered in ExtJS.
     * This method can also return the generated javascript instead of
     * sending it to the main javascript generator.
     * @param string $title title of the message box.
     * @param string $message the message to show.
     * @param string $icon the message box icon.
     * @param string $handler a javascript function to run after a button click.
     * @param bool $return set to true if the actual generated javascript is required, defaults to false.
     * @param string $scope the scope in which this message box will run, defaults to null (current browser window).
     * @return string/null returns the generated message box if return is set to true otherwise null, defaults to false.
     */
    public static function alert($title,$message,$icon=null,$handler=null,$return=false,$scope=null)
    {
        $config = new ExtJavaScriptObject();
        $config->addString('title', $title);
        $config->addString('msg', $message);
        $config->addMember('buttons', ExtMessageBox::$BUTTON_OK);
        ExtMessageBox::setScopeAndHandler($config, $handler, $scope);
        ExtMessageBox::setIcon($config, $icon);
        $msg = ExtMessageBox::render($config);
        if($return)
            return $msg;
        else
            ExtMessageBox::commitAjax($msg);
        return null;
    }

    /**
     * Creates a server-size confirm message box to be rendered in ExtJS.
     * This method can also return the generated javascript instead of 
     * sending it to the main javascript generator.
     * @param string $title title of the message box.
     * @param string $message the message to show.
     * @param string $handler a javascript function to run after a button click.
     * @param bool $return set to true if the actual generated javascript is required, defaults to false.
     * @param string $scope the scope in which this message box will run, defaults to null (current browser window)
     * @return string/null returns the generated message box if return is set to true otherwise null, defaults to false. 
     */
    public static function confirm($title,$message,$handler=null,$return=false,$scope=null)
    {
        $config = new ExtJavaScriptObject();
        $config->addString('title', $title);
        $config->addString('msg', $message);
        $config->addMember('buttons', ExtMessageBox::$BUTTON_YESNO);
        $config->addMember('icon', ExtMessageBox::$ICON_QUESTION);
        ExtMessageBox::setScopeAndHandler($config, $handler, $scope);
        $msg = ExtMessageBox::render($config);
        if($return)
            return $msg;
        else
            ExtMessageBox::commitAjax($msg);
        return null;
    }

    /**
     * Creates a server-size confirm message box with a cancel button to be rendered in ExtJS.
     * This method can also return the generated javascript instead of
     * sending it to the main javascript generator.
     * @param string $title title of the message box.
     * @param string $message the message to show.
     * @param string $handler a javascript function to run after a button click
     * @param bool $return set to true if the actual generated javascript is required, defaults to false.
     * @param string $scope the scope in which this message box will run, defaults to null (current browser window).
     * @return string/null returns the generated message box if return is set to true otherwise null, defaults to false.
     */
    public static function confirmWithCancel($title,$message,$handler=null,$return=false,$scope=null)
    {
        $config = new ExtJavaScriptObject();
        $config->addString('title', $title);
        $config->addString('msg', $message);
        $config->addMember('buttons', ExtMessageBox::$BUTTON_YESNOCANCEL);
        $config->addMember('icon', ExtMessageBox::$ICON_QUESTION);
        ExtMessageBox::setScopeAndHandler($config, $handler, $scope);
        $msg = ExtMessageBox::render($config);
        if($return)
            return $msg;
        else
            ExtMessageBox::commitAjax($msg);
        return null;
    }

    /**
     * Creates a single-line server-size prompt message box to be rendered in ExtJS.
     * This method can also return the generated javascript instead of
     * sending it to the main javascript generator.
     * @param string $title title of the message box.
     * @param string $message the message to show.
     * @param string $value the initial value to be set on when prompting, defaults to ""
     * @param string $handler a javascript function to run after a button click.
     * @param bool $return set to true if the actual generated javascript is required, defaults to false.
     * @param string $scope the scope in which this message box will run, defaults to null (current browser window)
     * @return string/null returns the generated message box if return is set to true otherwise null.
     */
    public static function prompt($title,$message,$value='',$handler=null,$return=false,$scope=null)
    {
        $config = new ExtJavaScriptObject();
        $config->addString('title', $title);
        $config->addString('msg', $message);
        $config->addMember('buttons', ExtMessageBox::$BUTTON_OKCANCEL);
        $config->addBoolean('prompt', true);
        $config->addMember('minWidth', 300);
        $config->addString('value', $value);
        ExtMessageBox::setScopeAndHandler($config, $handler, $scope);
        $msg = ExtMessageBox::render($config);
        if($return)
            return $msg;
        else
            ExtMessageBox::commitAjax($msg);
        return null;
    }

    /**
     * Creates a multiline-line server-size prompt message box to be rendered in ExtJS.
     * This method can also return the generated javascript instead of
     * sending it to the main javascript generator.
     * @param string $title title of the message box.
     * @param string $message the message to show.
     * @param string $value the initial value to be set on when prompting, defaults to ""
     * @param string $handler a javascript function to run after a button click.
     * @param bool $return set to true if the actual generated javascript is required, defaults to false.
     * @param string $scope the scope in which this message box will run, defaults to null (current browser window)
     * @return string/null returns the generated message box if return is set to true otherwise null.
     */
    public static function promptMultiLine($title,$message,$value='',$handler=null,$return=false,$scope=null)
    {
        $config = new ExtJavaScriptObject();
        $config->addString('title', $title);
        $config->addString('msg', $message);
        $config->addMember('buttons', ExtMessageBox::$BUTTON_OKCANCEL);
        $config->addBoolean('prompt', true);
        $config->addBoolean('multiline', true);
        $config->addMember('minWidth', 300);
        $config->addString('value', $value);
        ExtMessageBox::setScopeAndHandler($config, $handler, $scope);
        $msg = ExtMessageBox::render($config);
        if($return)
            return $msg;
        else
            ExtMessageBox::commitAjax($msg);
        return null;
    }

    /**
     * Creates a server-size progress message box to be rendered in ExtJS.
     * This method can also return the generated javascript instead of
     * sending it to the main javascript generator
     * @param string $title title of the message box
     * @param string $message the message to show
     * @param <type> $progressText text to be shown when progressbar is moving
     * @param string $icon the message box icon
     * @param bool $return set to true if the actual generated javascript is required.
     * @return string/null returns the generated message box if return is set to true otherwise null.
     */
    public static function progress($title,$message,$progressText,$icon=null,$return=false)
    {
        $config = new ExtJavaScriptObject();
        $config->addString('title', $title);
        $config->addString('msg', $message);
        $config->addString('progressText', $progressText);
        $config->addBoolean('progress', true);
        $config->addBoolean('closable', false);
        $config->addMember('minWidth', 300);
        ExtMessageBox::setIcon($config, $icon);
        $msg = ExtMessageBox::render($config);
        if($return)
            return $msg;
        else
            ExtMessageBox::commitAjax($msg);
        return null;
    }

    /**
     * Creates a server-size wait message box to be rendered in ExtJS.
     * This method can also return the generated javascript instead of
     * sending it to the main javascript generator.
     * For closing this message box on client side, please call "Ext.MessageBox.hide();"
     * @param string $message the message to show.
     * @param <type> $progressText text to be shown when progressbar is moving.
     * @param string $icon the message box icon.
     * @param bool $return set to true if the actual generated javascript is required, defaults to false.
     * @return string/null returns the generated message box if return is set to true otherwise null.
     */
    public static function wait($message,$progressText,$icon=null,$return=false)
    {
        $config = new ExtJavaScriptObject();
        $config->addString('msg', $message);
        $config->addString('progressText', $progressText);
        $config->addBoolean('wait', true);
        $config->addMember('waitConfig', '{interval:200}');
        $config->addMember('minWidth', 300);
        ExtMessageBox::setIcon($config, $icon);
        $msg = ExtMessageBox::render($config);
        if($return)
            return $msg;
        else
            ExtMessageBox::commitAjax($msg);
        return null;
    }

    private static function setScopeAndHandler($config,$handler,$scope)
    {
        if(!is_null($handler))
            $config->addMember('fn', $handler);

        if(!is_null($scope))
            $config->addMember('scope', $scope);
    }

    private static function setIcon($config,$icon)
    {
        if(is_null($icon))
            return;

        if(
        $icon != ExtMessageBox::$ICON_QUESTION &&
            $icon != ExtMessageBox::$ICON_WARNING &&
            $icon != ExtMessageBox::$ICON_INFO &&
            $icon != ExtMessageBox::$ICON_ERROR
        )
            $config->addString('icon',$icon);
        else
            $config->addMember('icon',$icon);
    }

    private static function render($config)
    {
        return sprintf('Ext.MessageBox.show(%s)',$config->render());
    }

    private static function commitAjax($msg)
    {
        if(Yii::app()->getRequest()->isAjaxRequest)
        {
            Yii::app()->getController()->addClientAction($msg);
        }
        else
        {
            Yii::app()->getController()->addAfterInitScript($msg);
        }
    }
}
?>
